var xmlhttp;
var wordId = null;
var meaningCount = 1;
var totalAddedMeaning = 1;
var meaningArray = new Array(100);

function getXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return xmlhttp;
}


function addWord(category)
{
	getXmlHttpObject();
	var mainWord = document.getElementById("mainWord").value;
	var slug = document.getElementById("slug").value;
	var isUpdate = document.getElementById("isUpdate").value;
	var url = "../controllers/addWordController.php?word=" + mainWord + "&slug=" + slug + "&isUpdate=" +  isUpdate + "&category=" +  category;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = addWordSuccess;
	xmlhttp.send();
}

function addMeaning(meaningIndex)
{
	getXmlHttpObject();
	var chineseMeaning = document.getElementById("meaning_chinese_" + meaningIndex).value;
	var englishMeaning = document.getElementById("meaning_english_" + meaningIndex).value;
	if(chineseMeaning == "")
	{
		alert("中文含义不能为空");
		return;
	}
	if(englishMeaning == "")
	{
		alert("英文含义不能为空");
		return;
	}
	var url = "../controllers/addMeaningController.php?chineseMeaning=" + chineseMeaning + "&englishMeaning=" + englishMeaning + "&wordId=" +  wordId + "&meaningIndex=" +  meaningIndex;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = addMeaningSuccess;
	xmlhttp.send();
}

function addSynonym(meaningIndex)
{
	getXmlHttpObject();
	var synonym = document.getElementById("synonym_input_" + meaningIndex).value;
	var meaningDR = document.getElementById("meaningArea_" + meaningIndex);
	var url = "../controllers/addSynonymController.php?synonym=" + synonym + "&meaningIndex=" + meaningIndex + "&meaningId=" +  meaningArray[meaningIndex] + "&wordId=" +  wordId;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = addSynonymSuccess;
	xmlhttp.send();
	document.getElementById("synonym_input_" + meaningIndex).value = "";
}

function addAntonym(meaningIndex)
{
	getXmlHttpObject();
	var antonym = document.getElementById("antonym_input_" + meaningIndex).value;
	var url = "../controllers/addAntonymController.php?antonym=" + antonym + "&meaningIndex=" + meaningIndex + "&meaningId=" +  meaningArray[meaningIndex] + "&wordId=" +  wordId;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = addAntonymSuccess;
	xmlhttp.send();
	document.getElementById("antonym_input_" + meaningIndex).value = "";
}

function addSentence(meaningIndex)
{
	getXmlHttpObject();
	var sentence = document.getElementById("sentence_input_" + meaningIndex).value;
	var url = "../controllers/addSentenceController.php?sentence=" + sentence + "&meaningIndex=" + meaningIndex + "&meaningId=" +  meaningArray[meaningIndex] + "&wordId=" +  wordId;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = addSentenceSuccess;
	xmlhttp.send();
	document.getElementById("sentence_input_" + meaningIndex).value = "";
}

function swichWordOperationMode(mode)
{
	var area = document.getElementById("wordOperationArea");
	switch(mode)
	{	
	case 0:
		area.innerHTML = "<input type='button' value='添加单词' onclick='addWord();'></input>";
		break;
	case 1:
		area.innerHTML = "<input type='button' value='添加含义' onclick='addMeaningHTML();'></input>";
		area.innerHTML += "&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='修改单词' onclick='updateWord();'></input>";
		area.innerHTML += "&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='删除单词' onclick='deleteWord();'></input>";
		area.innerHTML += "&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='继续添加单词' onclick='window.location.reload();'></input>";
		break;
	default:
		break;
	}
}

function addWordSuccess() {
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			var responseText = xmlhttp.responseText;
			
			if(responseText.indexOf("error") != -1)
			{
				alert(responseText);
				return;
			}
			wordId = responseText;
			swichWordOperationMode(1);
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function addMeaningHTML()
{
	var wordTable = document.getElementById("wordTable");
	var newMeaningDR = "<td colspan='5' align='center'>"+
						"<table width='90%' rules='all' cellpadding='5%'>"+
							"<tr><th colspan='5' align='center' width='200'>含义" + totalAddedMeaning + "</input></th></tr>"+
							"<tr><th colspan='1'>中文含义</th>" + 
							"<td colspan='4'><input id='meaning_chinese_" + totalAddedMeaning + "' type='text' size=120></input></td></tr>"+
							"<tr><th colspan='1'>英文含义</th>" + 
							"<td colspan='4'><input id='meaning_english_" + totalAddedMeaning + "' type='text' size=120></input></td></tr>"+
							"<tr><td colspan='5' align='center'><input type='button' value='提交含义' onclick='addMeaning(" + totalAddedMeaning + ")'></input></td></tr>"+
							"<tr><th colspan='1'>同义词</th><td></td></tr>"+
							"<tr><th colspan='1'>反义词</th><td></td></tr>"+
							"<tr><th colspan='1'>例句</th><td></td></tr>"+
							"</table></td>";
	var insertRow = wordTable.insertRow(wordTable.rows.length);
	insertRow.id = "meaningArea_" + totalAddedMeaning;
	insertRow.innerHTML = newMeaningDR;
	meaningCount ++;
	totalAddedMeaning ++;
}


function addMeaningSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			var stringArray = xmlhttp.responseText.split("||");
			var chineseMeaning = stringArray[0];
			var englishMeaning = stringArray[1];
			var meaningId = stringArray[2];
			var meaningIndex = stringArray[3];
			meaningArray[meaningIndex] = meaningId;
			var meaningDR = document.getElementById("meaningArea_" + meaningIndex);
			var newMeaningDR = "<tr id = 'meaningArea_" + meaningIndex +"'><td colspan='5' align='center'>"+
								"<table width='90%' rules='all' cellpadding='5%'>"+
									"<tr><th colspan='5' align='center' width='200'>含义" + meaningIndex + "<input type='button' value='删除该含义' onclick='deleteMeaning(" + meaningIndex + ");'></input></th></tr>"+
									"<tr><th colspan='1'>中文含义</th>" + 
									"<td colspan='4'><input id='meaning_chinese_" + meaningIndex + "' type='text' size=120 value=" + chineseMeaning + "></input></td></tr>"+
									"<tr><th colspan='1'>英文含义</th>" + 
									"<td colspan='4'><input id='meaning_english_" + meaningIndex + "' type='text' size=120 value=" + englishMeaning + "></input></td></tr>"+
									"<tr><td colspan='5' align='center'><input type='button' value='修改含义' onclick='updateMeaning(" + meaningIndex + ")'></input></td></tr>"+									
									"<tr><th colspan='1'>同义词</th><td colspan='4'><table id='synonym_" + meaningIndex + "'>"+
									"<tr><td colspan='2'><input id='synonym_input_" + meaningIndex + "' type='text' size='20'></input></td></tr>"+
									"<tr><td colspan='2'><input type='button' value='添加同义词' onclick='addSynonym(" + meaningIndex + ");'></input></td></tr>"+
									"</table></td></tr>"+
									"<tr><th colspan='1'>反义词</th><td colspan='4'><table id='antonym_" + meaningIndex + "'>"+
									"<tr><td colspan='2'><input id='antonym_input_" + meaningIndex + "' type='text' size='20'></input></td></tr>"+
									"<tr><td colspan='2'><input type='button' value='添加反义词' onclick='addAntonym(" + meaningIndex + ");'></input></td></tr>"+
									"</table></td></tr>"+
									"<tr><th colspan='1'>例句</th><td colspan='4'><table id='sentence_" + meaningIndex + "'>"+
									"<tr><td colspan='2'><input id='sentence_input_" + meaningIndex + "' type='text' size='120'></input></td></tr>"+
									"<tr><td colspan='2'><input type='button' value='添加例句' onclick='addSentence(" + meaningIndex + ");'></input></td></tr>"+
									"</table></td></tr>"+
									"</table></td></tr>";
			meaningDR.innerHTML = newMeaningDR;
		}

	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}


function addSynonymSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			var stringArray = xmlhttp.responseText.split("||");
			var synonym = stringArray[0];
			var synonymId = stringArray[1];
			var meaningIndex = stringArray[2];
			
			var synonymTable = document.getElementById("synonym_" + meaningIndex);
			var insertRow = synonymTable.insertRow(0);
			insertRow.id = synonymId;
			insertRow.innerHTML = "<td>" + synonym + "</td>" + "<td><input type='button' value='删除' onclick='deleteSynonym(" + meaningIndex + "," + synonymId + ");'></td>";
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function addAntonymSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			var stringArray = xmlhttp.responseText.split("||");
			var antonym = stringArray[0];
			var antonymId = stringArray[1];
			var meaningIndex = stringArray[2];

			var antonymTable = document.getElementById("antonym_" + meaningIndex);
			var insertRow = antonymTable.insertRow(0);
			insertRow.id = antonymId;
			insertRow.innerHTML = "<td>" + antonym + "</td>" + "<td><input type='button' value='删除' onclick='deleteAntonym(" + meaningIndex + "," + antonymId + ");'></td>";
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function addSentenceSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			var stringArray = xmlhttp.responseText.split("||");
			var sentence = stringArray[0];
			var sentenceId = stringArray[1];
			var meaningIndex = stringArray[2];

			var sentenceTable = document.getElementById("sentence_" + meaningIndex);
			var insertRow = sentenceTable.insertRow(0);
			insertRow.id = sentenceId;
			insertRow.innerHTML = "<td>" + sentence + "</td>" + "<td><input type='button' value='删除' onclick='deleteSentence(" + meaningIndex + "," + sentenceId + ");'></td>";
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function deleteWord()
{
	if(confirm('确实要删除吗?'))
	{
		getXmlHttpObject();
		var url = "../controllers/deleteWordController.php?wordId=" + wordId;
		xmlhttp.open("GET",url,true);
		xmlhttp.onreadystatechange = deleteWordSuccess;
		xmlhttp.send();
	}
}

function deleteWordSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			var category = document.getElementById("category").value;
			window.location.href = "main.php?category=" + category;
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function updateWord(category)
{
	getXmlHttpObject();
	var mainWord = document.getElementById("mainWord").value;
	var slug = document.getElementById("slug").value;
	var isUpdate = document.getElementById("isUpdate").value;
	var url = "../controllers/updateWordController.php?wordId=" + wordId + "&word=" + mainWord + "&slug=" + slug + "&isUpdate=" +  isUpdate  + "&category=" +  category;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = updateWordSuccess;
	xmlhttp.send();
}

function updateWordSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			alert("更新单词成功!");
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function deleteMeaning(meaningIndex)
{
	getXmlHttpObject();
	var url = "../controllers/deleteMeaningController.php?meaningId=" + meaningArray[meaningIndex] + "&wordId=" +  wordId;
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
	
	var wordTable = document.getElementById("wordTable");
	var rowCount = wordTable.rows.length;
	var rowIndex = rowCount - meaningCount + meaningIndex;
	var meaningId = meaningArray[meaningIndex];
	wordTable.deleteRow(rowIndex);
	meaningCount --;
}

function updateMeaning(meaningIndex)
{
	getXmlHttpObject();
	var chineseMeaning = document.getElementById("meaning_chinese_" + meaningIndex).value;
	var englishMeaning = document.getElementById("meaning_english_" + meaningIndex).value;
	var meaningId = meaningArray[meaningIndex];
	if(chineseMeaning == "")
	{
		alert("中文含义不能为空");
		return;
	}
	if(englishMeaning == "")
	{
		alert("英文含义不能为空");
		return;
	}
	var url = "../controllers/updateMeaningController.php?meaningId=" + meaningId + "&chineseMeaning=" + chineseMeaning + "&englishMeaning=" + englishMeaning + "&wordId=" +  wordId + "&meaningIndex=" +  meaningIndex;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = updateMeaningSuccess;
	xmlhttp.send();
}

function updateMeaningSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			alert("更新含义成功!");
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function deleteSynonym(meaningIndex, synonymId)
{
	getXmlHttpObject();
	var url = "../controllers/deleteSynonymController.php?synonymId=" + synonymId + "&wordId=" +  wordId;
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
	
	var synonymTable = document.getElementById("synonym_" + meaningIndex);
	var findDeleteRow = false;
	var deleteIndex = 0;
	for(var i= 0 ; i < synonymTable.rows.length; i++)
	{
		var row = synonymTable.rows[i];
		if(row.id == synonymId)
		{
			findDeleteRow = true;
			break;
		}
		deleteIndex ++;
	}
	if(findDeleteRow == true)
	{
		synonymTable.deleteRow(deleteIndex);
	}
}

function deleteAntonym(meaningIndex, antonymId)
{	
	getXmlHttpObject();
	var url = "../controllers/deleteAntonymController.php?antonymId=" + antonymId + "&wordId=" +  wordId;
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
	
	var antonymTable = document.getElementById("antonym_" + meaningIndex);
	var findDeleteRow = false;
	var deleteIndex = 0;
	for(var i= 0 ; i < antonymTable.rows.length; i++)
	{
		var row = antonymTable.rows[i];
		if(row.id == antonymId)
		{
			findDeleteRow = true;
			break;
		}
		deleteIndex ++;
	}
	if(findDeleteRow == true)
	{
		antonymTable.deleteRow(deleteIndex);
	}
}

function deleteSentence(meaningIndex, sentenceId)
{
	getXmlHttpObject();
	var url = "../controllers/deleteSentenceController.php?sentenceId=" + sentenceId + "&wordId=" +  wordId;
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
	
	var sentenceTable = document.getElementById("sentence_" + meaningIndex);
	var findDeleteRow = false;
	var deleteIndex = 0;
	for(var i= 0 ; i < sentenceTable.rows.length; i++)
	{
		var row = sentenceTable.rows[i];
		if(row.id == sentenceId)
		{
			findDeleteRow = true;
			break;
		}
		deleteIndex ++;
	}
	if(findDeleteRow == true)
	{
		sentenceTable.deleteRow(deleteIndex);
	}
}

function showDefaultHTML(category)
{
	document.write ("<div id='mainDiv'>"+
							"<a href='main.php?category=" + category + "'>返回管理主页</a><hr/>"+
							"<table id='wordTable' border='2' rules='rows' cellpadding='8%' align='center' width='80%'>"+
								"<tr><th>单词</th><td colspan='4'><input id='mainWord' type='text' size=20></input></td></tr>"+
								"<tr><th>音标</th><td colspan='4'><input id='slug' type='text' size=20></input></td></tr>"+
								"<tr><th>更新词汇</th><td colspan='4'><select id='isUpdate'><option selected='selected' value='0'>否</option><option value='1'>是</option></select></td></tr>"+
								"<tr><td colspan='5' align='center' height='30'>"+
									"<div id='wordOperationArea'><input type='button' value='添加单词'  style='height:25px; width:100px;' onclick='addWord(" + category + ");'></input></div>"+
								"</td></tr>"+
								"<tr><td colspan='5' style='background-color:gray'></td></tr>"+			
								"</table></div>" +
								"<input type='hidden' id='category' value='" + category + "'/>");
}
